<template>
  <!-- 基于 Element UI 新增和修改弹窗 -->
  <a-drawer
    :title="!dataForm.id ? '添加' : '修改'"
    :visible="visible"
    @close="cancel"
    :body-style="{ paddingBottom: '80px' }"
    :width="750"
  >
    <!-- 新增和创建表单表单 -->
    <el-form :model="dataForm" :rules="dataRule" v-loading="loading" ref="dataForm" @keyup.enter.native="dataSubmit()"
             label-width="120px" style="padding: 0px 10px;margin: 0px 10px">
      <el-form-item label="等级名称" prop="levelname">
        <el-input v-model="dataForm.levelname" placeholder="请输入等级名称" size="small"></el-input>
      </el-form-item>
      <el-form-item label="等级" prop="level">
        <el-input type="number" v-model="dataForm.level" placeholder="请输入等级" size="small"></el-input>
      </el-form-item>
      <el-form-item label="每日成长值"  prop="datelevelvalue">
        <el-input type="number"  v-model="dataForm.datelevelvalue" placeholder="请输入每日成长值" size="small"></el-input>
      </el-form-item>
      <el-form-item label="分钟增长值"  prop="minupvalue">
        <el-input type="number"  v-model="dataForm.minupvalue" placeholder="请输入分钟增长值" size="small"></el-input>
      </el-form-item>
      <el-form-item label="等级成长值"  prop="levelvalue">
        <el-input type="number"  v-model="dataForm.levelvalue" placeholder="请输入等级成长值" size="small"></el-input>
      </el-form-item>

    </el-form>
    <div
      :style="{position: 'absolute',right: 0,bottom: 0,width: '100%', borderTop: '1px solid #e9e9e9',padding: '10px 16px',background: '#fff',textAlign: 'right',zIndex: 1,}">
      <el-button @click="cancel()" :style="{ marginRight: '8px' }">取消</el-button>
      <el-button type="primary" @click="submit()">确定</el-button>
    </div>
  </a-drawer>
</template>

<script>
import api from '@/api/api'

export default {
  components: {},
  data() {
    return {
      visible: false,
      selOptions: {},
      loading: false,
      dataForm: {
        id: 0,
        levelname: '',
        level: '',
        datelevelvalue: '',
        levelvalue: '',
        levelicon: '',
        minupvalue: ''
      },
      dataRule: {
        levelname: [
          { required: true, message: '等级名称  为必填项', trigger: 'blur' }
        ],
        level: [
          { required: true, message: '等级  为必填项', trigger: 'blur' }
        ],
        datelevelvalue: [
          { required: true, message: '日成长值  为必填项', trigger: 'blur' }
        ],
        levelvalue: [
          { required: true, message: '成长值  为必填项', trigger: 'blur' }
        ],
        levelicon: [
          { required: true, message: 'icon  为必填项', trigger: 'blur' }
        ],
        createtime: [
          { required: true, message: '创建时间  为必填项', trigger: 'blur' }
        ],
        updatetime: [
          { required: true, message: '更新时间  为必填项', trigger: 'blur' }
        ],
        minupvalue: [
          { required: true, message: '分钟增长值  为必填项', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    init(id) { // 初始化表单验证规则
      this.dataForm.id = id || undefined
      this.visible = true
      this.selOptions = this.$constants.selOptions
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        if (this.dataForm.id) {
          this.loading = true
          api.post('rest/vabc/level/info', { id: this.dataForm.id }).then(res => {
            this.dataForm = res
          }).finally(() => {
            this.loading = false
          })
        }
      })
    },
    // 表单数据提交
    submit() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          this.loading = true
          let postData = Object.assign({}, this.dataForm)
          if (!this.dataForm.id) {
            postData.id == undefined
            api.post('rest/vabc/level/save', postData).then(res => {
              this.$message.success('创建成功')
              this.reset()
              this.visible = false
              this.$parent.getDataList()
            }).finally(() => {
              this.loading = false
            })
          } else {
            api.post('rest/vabc/level/update', postData).then(res => {
              this.$message.success('保存成功')
              this.reset()
              this.visible = false
              this.$parent.getDataList()
            }).finally(() => {
              this.loading = false
            })
          }
        }
      })
    },
    cancel() {
      this.visible = false
      this.reset()
    },
    reset() {
      this.$refs.dataForm.resetFields()
      this.$data.dataForm = JSON.parse(JSON.stringify(this.$options.data().dataForm))
      //图片回写
      //this.$refs["childData"].fileList=[]
    }
  }
}
</script>
